package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.BaseCategory1Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory3Mapper;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Attribute;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@Tag(name = "分类管理")
@RestController
@RequestMapping(value="/api/album")
@SuppressWarnings({"all"})
public class BaseCategoryApiController {

	@Autowired
	private BaseCategoryService baseCategoryService;
	//

	/**
	 * 根据一级分类id获取全部分类信息
	 * /api/album/category/getBaseCategoryList/{category1Id}
	 * @param category1Id
	 * @return
	 */
	@GetMapping("/category/getBaseCategoryList/{category1Id}")
	public Result<JSONObject> getBaseCategoryList(@PathVariable Long category1Id){

	 JSONObject jsonObject =	baseCategoryService.getBaseCategorys(category1Id);
	 return Result.ok(jsonObject);
	}

	/**
	 * 根据一级分类Id查询三级分类列表
	 * /api/album/category/findTopBaseCategory3/{category1Id}
	 * @param category1Id
	 * @return
	 */
	@GetMapping("category/findTopBaseCategory3/{category1Id}")
	public Result<List<BaseCategory3>> findTopBaseCategory3(@PathVariable Long category1Id){
		List<BaseCategory3> baseCategory3List= baseCategoryService.findTopBaseCategory3(category1Id);
		return Result.ok(baseCategory3List);
	}



	/**
	 * 根据三级分类Id 获取到分类信息
	 * /api/album/category/getCategoryView/{category3Id}
	 * @param category3Id
	 * @return
	 */

	@GetMapping("category/getCategoryView/{category3Id}")
	public Result<BaseCategoryView> getCategoryView(@PathVariable Long category3Id){
		BaseCategoryView baseCategoryView = baseCategoryService.getCategoryView(category3Id);

		return Result.ok(baseCategoryView);
	}


	/**
	 * 修改专辑
	 * /api/album/albumInfo/updateAlbumInfo/{id}
	 * @param id
	 * @param albumInfoVo
	 * @return
	 */
	@PutMapping("albumInfo/updateAlbumInfo/{id}")
	@GuiGuLogin
	public Result updateAlbumInfoById(@PathVariable Long id,@RequestBody AlbumInfoVo albumInfoVo){
		baseCategoryService.updateAlbumInfoById(id,albumInfoVo);
		return Result.ok();
	}


	/**
	 * 根据ID查询专辑信息
	 * 	/api/album/albumInfo/getAlbumInfo/{id}
	 * @param id
	 * @return
	 */
	@GetMapping("albumInfo/getAlbumInfo/{id}")
//	@GuiGuLogin
	public Result<AlbumInfo> getAlbumInfo(@PathVariable Long id){
		AlbumInfo albumInfo= baseCategoryService.getAlbumInfo(id);
		return Result.ok(albumInfo);
	}


	/**
	 * 根据ID删除专辑
	 * /api/album/albumInfo/removeAlbumInfo/{id}
	 * @param id
	 * @return
	 */
	@DeleteMapping("albumInfo/removeAlbumInfo/{id}")
	@GuiGuLogin
	public Result removeAlbumInfoByid(@PathVariable Long id){

		baseCategoryService.removeAlbumInfoByid(id);
		return  Result.ok();
	}

	/**
	 * 查看主播专辑信息
	 * api/album/albumInfo/findUserAlbumPage/{page}/{limit}
	 * @param page
	 * @param limit
	 * @param albumInfoQuery
	 * @return
	 */
	@GuiGuLogin
	@PostMapping("albumInfo/findUserAlbumPage/{page}/{limit}")
	public Result<Page<AlbumListVo>> findUserAlbumPage(@PathVariable Long page, @PathVariable Long limit, @RequestBody AlbumInfoQuery albumInfoQuery){
		Long userId = AuthContextHolder.getUserId();
		albumInfoQuery.setUserId(userId);
		Page<AlbumListVo> pageInfo = new Page<>(page, limit);
		pageInfo= baseCategoryService.findUserAlbumPage(pageInfo, albumInfoQuery);
		return Result.ok(pageInfo);
	}

	/**
	 * /api/album/albumInfo/saveAlbumInfo
	 * 保存专辑
	 * @param albumInfoVo
	 * @return
	 */

	@GuiGuLogin
	@PostMapping("albumInfo/saveAlbumInfo")
	public Result albumInfoBySaveAlbumInfo(@RequestBody AlbumInfoVo albumInfoVo){
		baseCategoryService.albumInfoBySaveAlbumInfo(albumInfoVo);
		return Result.ok();
	}


	/**
	 * 根据一级标题查找分类标签
	 * /api/album/category/findAttribute/{category1Id}
	 * @param category1Id
	 * @return
	 */
	@GetMapping("category/findAttribute/{category1Id}")
	public Result<List<BaseAttribute>> findAttributeByCategory1Id(@PathVariable Long category1Id){
		List<BaseAttribute>  baseAttributes = baseCategoryService.findAttributeByCategory1Id(category1Id);
		return Result.ok(baseAttributes);
	}

	/**
	 * 获取三级专辑信息
	 * @return
	 */
	@GetMapping("category/getBaseCategoryList")
	public Result<List<JSONObject>> getBaseCategoryList(){

		List<JSONObject> list=baseCategoryService.getBaseCategoryList();
		return Result.ok(list);
	}

}

